<?php
	class ReponseManager{
		
		  private $_db;
	
		  public function __construct($db){ // Constructeur
				$this->setDb($db);
		  }
		  
		  public function setDb(PDO $db){
				$this->_db = $db;
		  }
		  
		  public function add(Reponse $reponse){// permet d'ajouter une réponse
				$q = $this->_db->prepare('INSERT INTO reponse SET idRefQuestion = :idRefQuestion, contenu = :contenu, correcte = :correcte, image = :image, ordre = :ordre');
				$q->bindValue(':idRefQuestion', $reponse->idRefQuestion());
				$q->bindValue(':contenu', $reponse->contenu());
				$q->bindValue(':correcte', $reponse->correcte());
				$q->bindValue(':image', $reponse->image());
				
				$ordre = $this->getNbReponse($reponse->idRefQuestion());
				$ordre++;
				$q->bindValue(':ordre', $ordre);
				$q->execute();
				return $this->_db->lastInsertId();
		  }
		  
		  public function delete(Reponse $reponse){// permet de supprimer une réponse
			  $this->_db->exec('DELETE FROM reponse WHERE id = '.$reponse->id());
		  }
		  
		  public function update(Reponse $reponse){// permet de modifier une réponse
				$q = $this->_db->prepare('UPDATE reponse SET idRefQuestion = :idRefQuestion, contenu = :contenu, correcte = :correcte, image = :image, ordre = :ordre WHERE id = '.$reponse->id());
				$q->bindValue(':idRefQuestion', $reponse->idRefQuestion());
				$q->bindValue(':contenu', $reponse->contenu());
				$q->bindValue(':correcte', $reponse->correcte());
				$q->bindValue(':image', $reponse->image());
				$q->bindValue(':ordre', $reponse->ordre());	
				$q->execute();
		  }
		  
		  public function get($id){// retourne l'objet réponse d'id correspondant
				$id = (int) $id;
				$q = $this->_db->query('SELECT * FROM reponse WHERE id = '.$id);
				$donnees = $q->fetch();
				return new Reponse($donnees);
		  }
		  
		  public function getByQuestion($idQuestion){// retourne un tableau des réponses correspondant à une question.
				$reponse = array();
				$q = $this->_db->query('SELECT * FROM reponse WHERE idRefQuestion = '.$idQuestion.' ORDER BY ordre');
				while ($donnees = $q->fetch()){
					$reponse[] = new Reponse($donnees);
				}
				return $reponse;
		  }
		  
		  public function getNbReponse($idQuestion){// retourne le nombre de question d'un exercice
				$q = $this->_db->query('SELECT COUNT(*) FROM reponse WHERE idRefQuestion = '.$idQuestion);
				$donnees = $q->fetch();
				return $donnees[0];				  
		  }
		  
		
		public function ordreUp($id) { // Permet d'incrémenter l'ordre d'apparition de la réponse en fonction de l'id
			$reponse = $this->get($id);
			$ordreM = $reponse->ordre() - 1;
			$q = $this->_db->query('SELECT * FROM reponse WHERE idRefQuestion = '.$reponse->idRefQuestion().' AND ordre = '.$ordreM);
			
			if($q->rowCount() == 1)
			{
				$reponse2 = new Reponse($q->fetch());
				$reponse2->setOrdre($reponse2->ordre()+1);
				$reponse->setOrdre($reponse->ordre()-1);
				$this->update($reponse);
				$this->update($reponse2);
			}
		}
		
		public function ordreDown($id) { // Permet de décrémenter l'ordre d'appartition de la réponse en fonction de l'id
			$reponse = $this->get($id);
			$ordreP = $reponse->ordre() + 1;
			$q = $this->_db->query('SELECT * FROM reponse WHERE idRefQuestion = '.$reponse->idRefQuestion().' AND ordre = '.$ordreP);
			
			if($q->rowCount() == 1)
			{
				$reponse2 = new Reponse($q->fetch());
				$reponse2->setOrdre($reponse2->ordre()-1);
				$reponse->setOrdre($reponse->ordre()+1);
				$this->update($reponse);
				$this->update($reponse2);
			}
		}
		
		public function ReorganizeOrdre($idQuestion) //Reorganise l'ordre des réponses après une suppression
		{
			$i=1;
			foreach($this->getByQuestion($idQuestion) as $reponse)
			{
				$reponse->setOrdre($i);
				$this->update($reponse);
				$i++;
			}
		}
		
		public function getJuste($idQuestion)
		{
				$reponse = array();
				$q = $this->_db->query('SELECT * FROM reponse WHERE idRefQuestion = '.$idQuestion.' AND correcte = 1 ORDER BY ordre');
				while ($donnees = $q->fetch()){
					$reponse[] = new Reponse($donnees);
				}
				return $reponse;			
		}

	}
?>
